package org.example.everyday;

public class Lc1601 {
    class Result {
        int max = 0;

        public Result() {
            this.max = 0;
        }
    }



    public int maximumRequests(int n, int[][] requests) {
        Result res = new Result();
        dfs(res, requests, 0, new int[n], 0, 0);
        return res.max;
    }

    private void dfs(Result res, int[][] requests, int idx, int[] state, int approved, int nonZero) {
        System.out.println("经过了几次"+idx);
        // exit
        if(idx == requests.length) {

            // update result
            if(nonZero == 0) {
                res.max = Math.max(res.max, approved);
            }

            return;
        }

        // recur
        int[] request = requests[idx];


        // approve

        // update state
        state[request[0]]--;
        state[request[1]]++;
        int nextNonZero = getNextNonZero(state, request, nonZero);

        dfs(res, requests, idx + 1, state, approved + 1, nextNonZero);

        // revert
        state[request[0]]++;
        state[request[1]]--;

        // not approve
        dfs(res, requests, idx + 1, state, approved, nonZero);
//        dfs(res, requests, idx + 1, state, approved, nonZero);
    }

    private int getNextNonZero(int[] state, int[] request, int nonZero) {
        if(request[0] == request[1]) {
            return nonZero;
        }

        int nextNonZero = nonZero;
        if(state[request[0]] == -1) {
            nextNonZero++;
        }
        if(state[request[0]] == 0) {
            nextNonZero--;
        }
        if(state[request[1]] == 1) {
            nextNonZero++;
        }
        if(state[request[1]] == 0) {
            nextNonZero--;
        }
        return nextNonZero;
    }

    public static void main(String[] args) {
        Lc1601 lc1601 = new Lc1601();
        int n = 5;
        int[][] requests = {{0,1},{1,0},{1,2}};
        int result = lc1601.maximumRequests(requests.length,requests);
        System.out.println(result);
    }

}
